local tabletool = require("tabletool")
local filelog = require("filelog")
local math_random = math.random
local table_insert = table.insert
require "sgjenum"

local playruleconfig = {
    [1] = { -- 小猫变身概率(中小倍水果变成大倍水果)
        funcn = 1,
        registed = {[2101] = 1, [2102] = 1, [2103] = 1, [2104] = 1, [2105] = 1, [2106] = 1, [2107] = 1},
        val = 10,
    },
    [2] = { -- 中苹果触发{大四喜, 失败, 小满贯, 大满贯}
        funcn = 2,
        registed = {[1101] = 1,},
        total = 100, list = {dsx = 10, failed = 90, dmg = 0, xmg = 0},
    },
    [3] = { -- 中橘子，芒果，铃铛触发{小三元, 失败, 小满贯, 大满贯}
        funcn = 2,
        registed = {[1102] = 1, [1103] = 1, [1104] = 1,},
        total = 100, list = {xsy = 5, failed = 90, xmg = 5, dmg = 0},
    },    
    [4] = { -- 中西瓜，双星，双七触发{大三元, 失败, 小满贯, 大满贯}
        funcn = 2,
        registed = {[1105] = 1, [1106] = 1, [1107] = 1,},
        total = 100, list = {dsy = 5, failed = 90, xmg = 5, dmg = 0},
    },    
    [5] = { --大lucky
        funcn = 2,
        registed = {[1109] = 1,},
        total = 100, list = {sd = 40, lightning = 30, killall = 30, failed = 0},
        sd_total = 100,
        sd_list = {       --送灯权值表
            [3] = 50,
            [4] = 40,
            [5] = 10,
        },
        lightning_total = 100,
        lightning_list = {
            [2] = 50,
            [3] = 30,
            [4] = 15,
            [5] = 5,
            [6] = 0,
            [7] = 0,
            [8] = 0,
            [9] = 0,
        },
    },
    [6] = { --小lucky
        funcn = 2,
        registed = {[2109] = 1,},
        total = 100, list = {sd = 50, lightning = 0, killall = 50, failed = 0},
        sd_total = 100,
        sd_list = {       --送灯权值表
            [2] = 50,
            [3] = 40,
            [4] = 10,
        },
        lightning_total = 100,
        lightning_list = {
            
        },
    },
    [7] = { --大BAR
        funcn = 2,
        registed = {[1108] = 1,},
        total = 100, list = {sd = 50, failed = 50},
        sd_total = 100,
        sd_list = {       --送灯权值表
            [3] = 50,
            [4] = 40,
            [5] = 5,
            [6] = 5,
        },
    },
    [8] = { --BAR50
        funcn = 2,
        registed = {[2108] = 1,},
        total = 100, list = {sd = 50, failed = 50},
        sd_total = 100,
        sd_list = {       --送灯权值表
            [2] = 70,
            [3] = 30,
        },
    },
}

--送灯权值列表
local SD_weight_list = {
    [2107] = 600,  --77x2
    [2106] = 600,  --星星x2
    [2105] = 600,  --西瓜x2
    [2104] = 600,  --铃铛x2
    [2103] = 600,  --芒果x2
    [2102] = 600,  --橘子x2
    [2101] = 600,  --苹果x2

    [1104] = 1440, --铃铛大
    [1103] = 720,  --芒果大
    [1102] = 720,  --橘子大
    [1101] = 720,  --苹果大
}

--
--计算总权值
--
local function calculate_total_weight(wlist)
    local total = 0
    for id, w in pairs(wlist) do
        total = total + w
    end
    return total
end

--根据权值表，获取随机结果
local function get_random_by_weightlist(wlist, wtotal)
    wtotal = wtotal or calculate_total_weight(wlist)
    local r = math_random(wtotal)
    for k, v in pairs(wlist) do
        r = r - v
        if r <= 0 then
            -- filelog.debug("k =======>", k)
            return k
        end
    end
    -- filelog.debug("get_random_by_weightlist: find no func!", r, wtotal, wlist)
    return "failed"
end

local logic = {
    -- 失败
    failed = function()
        return false
    end,
    -- 小猫变身：中小倍水果变成大倍水果
    cat = function(cfg, prizeid)
        if math_random(100) <= (cfg.val or 10) then
            return {prizeid - 1000}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_CAT
        end
    end,
    -- 小四喜：?
    xsx = function()
        return false, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_XSX
    end,
    -- 大四喜：四个苹果
    dsx = function()
        return {1101, 1101, 1101, 1101}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_DSX
    end,
    -- 小三元：铃铛，芒果，橘子
    xsy = function()
        return {1102, 1103, 1104}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_XSY
    end,
    -- 大三元：双七，双星，西瓜
    dsy = function()
        return {1105, 1106, 1107}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_DSY
    end,
    -- 小满贯：所有小倍水果
    xmg = function()
        return {2101, 2102, 2103, 2104, 2105, 2106, 2107}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_XMG
    end,
    -- 大满贯：除Lucky的所有灯
    dmg = function()
        return {1101, 1101, 1101, 1101, 2101, 1102, 1102, 2102, 1103, 1103, 2103, 1104, 1104, 2104, 1105, 2105, 1106, 2106, 1107, 2107, 1108, 2108}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_DMG
    end,
    -- 送灯
    sd = function(cfg)
        local num = get_random_by_weightlist(cfg.sd_list, cfg.sd_total)
        local temp_sd_wlist = tabletool.deepcopy(SD_weight_list)
        local prizelist = {}
        for i = 1, num do
            local id = get_random_by_weightlist(temp_sd_wlist)
            table_insert(prizelist, id)
            temp_sd_wlist[id] = nil
        end
        return prizelist, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_GIVE_LAMP
    end,
    -- 闪电：下注总额随机翻2-9倍
    lightning = function(cfg)
        return get_random_by_weightlist(cfg.lightning_list, cfg.lightning_total), ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_LIGHTNING
    end,
    -- 庄家通杀：获得闲家全部下注
    killall = function()
        return {}, ESGJExtraPrizeType.EXTRA_PRIZE_TYPE_KILL_ALL
    end,
}

local func = {
    [1] = function(cfg, prizeid)
        -- filelog.debug("k =======>  cat")
        return logic.cat(cfg, prizeid)
    end,

    [2] = function(cfg, prizeid)
        return logic[get_random_by_weightlist(cfg.list, cfg.total)](cfg)
    end,
}

logic.process = function(prizeid)
    local prizedata = {prizelist = {prizeid}, extprizeitem = {}}
    local extprizelist, extprizetype
    for _, cfg in ipairs(playruleconfig) do
        if cfg.registed[prizeid] then
            if cfg.funcn then
                extprizelist, extprizetype = func[cfg.funcn](cfg, prizeid)
                if extprizelist and extprizetype then
                    --[[ 现在不支持一个结果触发多个奖励
                    table_insert(prizedata.extprizeitem, {
                        extprize_type = extprizetype,
                        extprize_list = extprizelist,
                    })
                    --]]

                    return {
                        extprize_type = extprizetype,
                        extprize_list = extprizelist,
                    }
                end
            end
        end
    end
end

return logic